<?php

namespace app\home\logic;

use think\Model;
use think\db;
use app\common\logic\CommentLogic;

/**
 * 分类逻辑定义
 * Class CatsLogic
 * @package Home\Logic
 */
class UsersLogic extends Model {

    /**
     * 检查短信/邮件验证码验证码
     * @param unknown $code
     * @param unknown $sender
     * @param unknown $session_id
     * @return multitype:number string
     */
    public function check_validate_code($code, $sender ,$scene = -1){

        $timeOut = time();
        $inValid = true;  //验证码失效

        //短信发送否开启
        //-1:用户没有发送短信
        //空:发送验证码关闭
        $sms_status = checkEnableSendSms($scene);

        if($scene == -1){
            return array('status'=> 500,'msg'=>'参数错误');
        }elseif($sms_status['status'] == 0){
            $data['sender'] = $sender;
            $data['is_check'] = 1; //标示验证通过
            session('validate_code',$data);
            return array('status'=> 200,'msg'=>'短信验证码功能关闭, 无需校验验证码');
        }

        if(!$code)return array('status'=> 500,'msg'=>'请输入短信验证码');
        //短信
        $sms_time_over = tpCache('sms.sms_time_over');
        $sms_time_over = $sms_time_over ? $sms_time_over : 180;
        $data = M('sms_log')->where(array('mobile'=>$sender, 'status'=>1))->order('id DESC')->find();
        if(is_array($data) && $data['code'] == $code){
            $data['sender'] = $sender;
            $timeOut = $data['add_time']+ $sms_time_over;
        }else{
            $inValid = false;
        }

        if(empty($data)){
            $res = array('status' => 500,'msg'=>'请先获取验证码');
        }elseif($timeOut < time()){
            $res = array('status' => 500,'msg'=>'验证码已超时失效');
        }elseif(!$inValid){
            $res = array('status'=> 500,'msg'=>'验证失败,验证码有误');
        }else{
            $data['is_check'] = 1; //标示验证通过
            session('validate_code',$data);
            $res = array('status'=> 200,'msg'=>'验证成功');
        }
        return $res;
    }


}